package com.dex4d.core;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dex4d.R;
import com.dex4d.ui.dapp.model.TabDappEvent;
import com.wallet.web3.entity.Web3Transaction;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * Created by chengyuchun on 2018/10/19.
 */
public abstract class AppToolBarBaseActivity extends AppBaseActivity {
    private LinearLayout mToolbarRootView;
    public Toolbar mToolbar;

    @BindView(R.id.toolbar_left_img)
    ImageView mImgLeft;

    @BindView(R.id.toolbar_left)
    RelativeLayout mToolbarLeft;

    @BindView(R.id.toolbar_title)
    TextView mTvTitle;

    @BindView(R.id.toolbar_right)
    RelativeLayout mToolbarRight;

    @BindView(R.id.toolbar_right_tv)
    TextView mToolbarRightTv;

    @BindView(R.id.toolbar_right_img)
    ImageView mToolbarRightImg;

    private boolean isClosed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int rootLayoutId = getLayoutId();
        View layoutView = getLayoutInflater().inflate(rootLayoutId, null, false); //layout view

        mToolbarRootView = new LinearLayout(this);
        mToolbarRootView.setOrientation(LinearLayout.VERTICAL);

        mToolbar = (Toolbar) View.inflate(this, R.layout.base_toolbar, null);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mToolbarRootView.addView(mToolbar);
        mToolbarRootView.addView(layoutView);

        super.onCreate(savedInstanceState);
        initToolbar();
    }

    protected abstract void initToolbar();

    @Override
    protected View getLayoutView() {
        return mToolbarRootView;
    }

    protected void showBackView() {
        mToolbarLeft.setVisibility(View.VISIBLE);
        mToolbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void showDappBackView(Web3Transaction transaction) {
        mToolbarLeft.setVisibility(View.VISIBLE);
        mToolbarLeft.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("TX", transaction);
            intent.putExtra("SIGNED", false);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    protected void showRightDone() {
        mToolbarRight.setVisibility(View.VISIBLE);
        mToolbarRightTv.setText(getString(R.string.done));
        mToolbarRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public RelativeLayout getToolbarRight() {
        return mToolbarRight;
    }

    public void hideBackView() {
        mToolbarLeft.setVisibility(View.GONE);
    }

    public void showTitle(String title) {
        mTvTitle.setText(title);
    }

    /**
     * 切换到收藏夹标签
     */
    protected void showRightImage() {
        mToolbarLeft.setVisibility(View.VISIBLE);
        mToolbarRightImg.setVisibility(View.VISIBLE);
        mToolbarRightImg.setImageResource(R.drawable.dapp_browser_deleted);

        //展示选择按钮
        mToolbarRightImg.setOnClickListener(v -> {
            mToolbarRightImg.setVisibility(View.GONE);
            mToolbarRightTv.setVisibility(View.VISIBLE);
            mToolbarRightTv.setText("Select all");
            mImgLeft.setBackgroundResource(R.drawable.dapp_bookmark_closed);
            isClosed = true;
            EventBus.getDefault().post(new TabDappEvent(0));
        });

        //全选按钮
        mToolbarRightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new TabDappEvent(1));
            }
        });

        mToolbarLeft.setOnClickListener(v -> {

            if (isClosed) {
                isClosed = false;
                mImgLeft.setBackgroundResource(R.drawable.ic_toolbar_back);
                mToolbarRightTv.setVisibility(View.GONE);
                mToolbarRightImg.setVisibility(View.VISIBLE);

                EventBus.getDefault().post(new TabDappEvent(4));
            } else {
                finish();
            }

        });
    }

    /**
     * 切换到历史记录标签
     */
    protected void slectedHistoryTag() {
        mImgLeft.setBackgroundResource(R.drawable.ic_toolbar_back);
        mToolbarRightImg.setVisibility(View.GONE);
        mToolbarRightTv.setVisibility(View.GONE);
        isClosed = false;
    }

    protected void  dappReset(){
        mImgLeft.setBackgroundResource(R.drawable.ic_toolbar_back);
        mToolbarRightImg.setVisibility(View.VISIBLE);
        mToolbarRightTv.setVisibility(View.GONE);
        isClosed = false;
    }
}
